package com.tfy.liquor.controller;

import com.tfy.liquor.bean.Result;
import com.tfy.liquor.bean.SystemConfiguration;
import com.tfy.liquor.service.SnowFlakeService;
import com.tfy.liquor.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 雪花算法工具
 */
@RestController
@RequestMapping("/snowFlake")
public class SnowFlakeController {
    @Autowired
    private SnowFlakeService snowFlakeService;

    // 获取默认id
    @RequestMapping("/getDefaultId")
    public Result getDefaultId() {
        Map<String, String> map = new HashMap();
        map.put("id", snowFlakeService.getDefaultId() + "");
        return new Result(map).ok("获取成功！");
    }

    // 传入工作id和数据中心id获取id
    @RequestMapping("/getId")
    public Result getId(Long workerId, Long datacenterId) {
        if (null == workerId || null == datacenterId || workerId < 0 || datacenterId < 0 || workerId > 31 || datacenterId > 31) {
            return new Result().bad_CodeIsNull();
        }
        Map<String, String> map = new HashMap();
        map.put("id", snowFlakeService.getId(workerId, datacenterId) + "");
        return new Result(map).ok("获取成功！");
    }
}
